<script setup lang="ts">
import { ref, onMounted, reactive } from "vue";
import { getCodeInfo, login } from "@/services/userService/lcdpLogin";
import { useUserStore } from "@/store/modules/user";
import { useRouter } from "vue-router";
const router = useRouter();
const sessionId = ref("");
const imageBase64 = ref("");
const formState = reactive({
  code: "",
  passwd: "!RPsPRExe4gt",
  userName: "chengxianglei",
});
const userStore = useUserStore();

const fetchCode = async () => {
  // 获取图片验证码
  sessionId.value = (+new Date()).toString();
  const res = await getCodeInfo(sessionId.value);
  imageBase64.value = res.image;
};

onMounted(() => {
  fetchCode();
});

const onSubmit = async () => {
  try {
    const loginInfo = await login({
      hasManager: false,
      hasInside: true,
      sessionId: sessionId.value,
      ...formState,
    });
    userStore.setLoginInfo(loginInfo);
    const tenantList = loginInfo.tenantList ?? [];
    userStore.setTenantId(tenantList[0]?.tenantId ?? "");
    router.push("/base/home");
  } catch (error) {
    fetchCode();
  }
};
</script>

<template>
  <div class="box">
    <div class="logo"></div>
    <van-form class="form" @submit="onSubmit">
      <van-cell-group inset>
        <van-field
          v-model="formState.userName"
          :rules="[{ required: true, message: '请输入用户名' }]"
          name="userName"
          placeholder="请输入用户名"
        >
          <template #left-icon>
            <van-icon name="user-o" color="#9CA8B3" />
          </template>
        </van-field>
        <van-field
          v-model="formState.passwd"
          :rules="[{ required: true, message: '请输入密码' }]"
          name="passwd"
          placeholder="请输入密码(6-14位)"
          type="password"
        >
          <template #left-icon>
            <van-icon name="bag-o" color="#9CA8B3" />
          </template>
        </van-field>
        <van-field
          v-model="formState.code"
          :rules="[{ required: true, message: '请输入验证码' }]"
          name="code"
          placeholder="请输入验证码"
          class="code-cell"
        >
          <template #left-icon>
            <van-icon name="orders-o" color="#9CA8B3" />
          </template>
          <template #button>
            <van-image
              class="code"
              @click="fetchCode"
              :src="`data:image/jpg;base64,${imageBase64}`"
            />
          </template>
        </van-field>
        <div class="btn">
          <van-button round block type="primary" native-type="submit"
            >登录</van-button
          >
          <div class="btn-bottom">
            <span class="wj">忘记密码？</span>
            <span class="zc">注册新账号</span>
          </div>
        </div>
      </van-cell-group>
    </van-form>
    <div class="foot">
      <img src="@/assets/login/img-1.png" alt="1" />
    </div>
  </div>
</template>

<style scoped lang="scss">
.box {
  background: #fff;
  height: 100vh;
  padding-top: 12vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  box-sizing: border-box;
  position: relative;
  .logo {
    width: 78px;
    height: 78px;
    background: #856fff;
    border-radius: 39;
    box-shadow: 0px 2px 6px 0px rgba(119, 119, 255, 0.35);
  }

  .form {
    width: 95%;
    margin-top: 24px;
    :deep(.van-cell) {
      margin-top: 10px;
    }
    :deep(.van-field__left-icon) {
      margin-right: 22px;
    }

    :deep(.van-button--primary) {
      background: #856fff;
      border: #856fff;
    }
    .code {
      margin-right: 10px;
      width: 50px;
      object-fit: cover;
    }
    .code-cell {
      :deep(.van-field__left-icon) {
        display: flex;
        align-items: center;
      }
    }
  }

  .btn {
    margin-top: 4px;

    .btn-bottom {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-top: 24px;
      font-size: 12px;
      font-weight: 400;
      line-height: 18px;
      .wj {
        color: #999999;
      }
      .zc {
        color: #856fff;
      }
    }
  }

  .foot {
    position: absolute;
    bottom: 0;
    width: 100%;
    img {
      width: 100%;
      height: 100%;
      vertical-align: bottom;
      object-fit: cover;
    }
  }
}
</style>
